<%--
  Created by IntelliJ IDEA.
  User: 24258
  Date: 2022/8/30
  Time: 23:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<script type="text/javascript" src="crowd/my-role.js"></script>
<script type="text/javascript" src="crowd/my-assign.js"></script>
<script type="text/javascript" src="crowd/my-common.js"></script>
<script type="text/javascript">
    $(function () {
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";
        generatePage();

        $("#searchBtn").click(function () {
            window.keyword = $("#keywordInput").val();
            window.pageNum = 1;
            generatePage();
        })

        $("#roleAddBtn").click(function () {
            $('#modelRoleAdd').modal('show');
        })

        // 添加
        $("#saveRoleBtn").click(function () {
            $.ajax({
                url: "role/save.json",
                type: "post",
                dataType: "json",
                data:
                    {
                        "name": $.trim($("#addRoleName").val())
                    },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("添加成功");
                        window.pageNum = 9999999;
                        // 清理模态框
                        $("#addRoleName").val("");
                        // 重新加载页面
                        generatePage();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("添加失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            });
            // 关闭模态框
            $('#modelRoleAdd').modal('hide');
        })

        $("#rolePageBody").on("click", ".pencilBtn", function () {
            // 要修改的用户值
            var name = $(this).parent().prev().text();
            // 要修改的用户id
            // var id = $(this).attr("id");
            var id = this.id;
            $("#modelRoleEdit [name=roleId]").val(id);
            $("#modelRoleEdit [name=roleName]").val(name);
            $("#modelRoleEdit").modal("show");
        })

        $("#updateRoleBtn").click(function () {
            $.ajax({
                url: "role/update.json",
                type: "post",
                dataType: "json",
                data: {
                    "id": $("#modelRoleEdit [name=roleId]").val(),
                    "name": $.trim($("#modelRoleEdit [name=roleName]").val())
                },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("更新成功");

                        // 重新加载页面
                        generatePage();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("更新失败" + response.message);
                        return;
                    }
                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            });
            // 关闭模态框
            $('#modelRoleEdit').modal('hide');
        })


        //模态框确认删除按钮
        $("#removeRoleBtn").click(function () {

            var requestBody = JSON.stringify(window.roleIdArray);

            $.ajax({
                url: "role/remove/by/role/id/array.json",
                type: "POST",
                data: requestBody,
                contentType: "application/json;charset=UTF-8",
                dataStyle: "json",
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("删除成功");
                        // 重新加载页面
                        generatePage();
                        return;
                    }
                    if (result == "FAILED") {
                        layer.msg("删除失败" + response.message);
                        return;
                    }
                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $('#modelRoleRemove').modal('hide');

        });

        // 单击动态数据绑定事件
        $("#rolePageBody").on("click", ".removeBtn", function () {

            var roleArray = [{
                roleId: this.id,
                roleName: $(this).parent().prev().text()
            }]

            showConfirmModel(roleArray);
        })

        // 总的checkbox绑定单击响应时间
        $("#summaryBox").click(function () {
            var currentStatus = this.checked;
            $(".itemBox").prop("checked", currentStatus);
        })

        // 全选，总的checkbox也选上
        $("#rolePageBody").on("click", ".itemBox", function () {
            var checkBoxCount = $(".itemBox:checked").length;
            var totalVoxCount = $(".itemBox").length;
            $("#summaryBox").prop("checked", checkBoxCount == totalVoxCount)
        })

        // 删除按钮
        $("#removeBtn").click(function () {
            var roleArray = [];
            $(".itemBox:checked").each(function () {
                var roleId = this.id;
                var roleName = $(this).parent().next().text();
                roleArray.push({
                    "roleId": roleId,
                    "roleName": roleName
                })
            })
            if (roleArray.length == 0) {
                layer.msg("请至少选择一个删除");
                return
            }
            showConfirmModel(roleArray);
        })

        // check按钮点击时间
        $("#rolePageBody").on("click", ".checkBtn", function () {
            window.id = this.id;
            $("#roleAssignModal").modal("show");
            fillAuthTree();
        })

        $("#assginAuthSaveBtn").click(function () {
            var zTree = $.fn.zTree.getZTreeObj("authTreeDemo");
            var checkedNodes = zTree.getCheckedNodes();
            // 不包含根节点的authId
            var noRootAuthIdArray = [];

            for (let i = 0; i < checkedNodes.length; i++) {
                var currentChecked = checkedNodes[i];
                // console.log(currentChecked.name);
                if(currentChecked.name != null && currentChecked.name != ""){
                    noRootAuthIdArray.push(currentChecked.id);
                }
            }

            var responseBody = {
                "roleId":[window.id],
                "AuthIdArray":noRootAuthIdArray
            }

            responseBody = JSON.stringify(responseBody);

            $.ajax({
                url:"assign/do/role/assign/auth.json",
                type:"post",
                data:responseBody,
                contentType: "application/json;charset=UTF-8",
                dataType:"json",
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {

                        layer.msg("保存成功");
                        // 重新加载页面
                        fillAuthTree();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("保存失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })

            $('#roleAssignModal').modal('hide');
        })

    })

</script>
<body>
<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="removeBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;">
                        <i class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button id="roleAddBtn" type="button" class="btn btn-primary" style="float:right;">
                        <i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            <%--显示内容--%>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/modal-role-add.jsp" %>
<%@include file="/WEB-INF/modal-role-edit.jsp" %>
<%@include file="/WEB-INF/modal-role-remove.jsp" %>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp" %>
</body>
</html>

